﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PoolSide
{
    public partial class frmMarket : Form
    {
        private ExchangeInfo Exchange;
        public frmMarket()
        {
            InitializeComponent();
        }
        public frmMarket(ExchangeInfo eInfo)
        {
            InitializeComponent();
            Exchange = eInfo;
            Exchange.OnEventChanged +=new ExchangeInfo.EventChanged(Exchange_OnEventChanged);
        }

        private void cmdClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmMarket_Load(object sender, EventArgs e)
        {
            if (Exchange == null)
                return;

            // if we don't have username for the exchange, then show the configuration tab.  otherwise show general tab:
            if (Exchange.Username == "null" || string.IsNullOrEmpty(Exchange.Username))
                tabMain.SelectedIndex = 3; // Configuration tab
            else
            {
                tabMain.SelectedIndex = 0; // General tab
                
                // contact exchange and load balances
                lblStatus.Text = "Getting account balances ...";

                if (string.IsNullOrEmpty(Exchange.Password))
                {
                    frmMarketPassword formPassword = new frmMarketPassword();
                    DialogResult dr = formPassword.ShowDialog();
                    if (dr == System.Windows.Forms.DialogResult.OK)
                        LoadExchangeInfo(Program.LastMarketPassword);
                }
                else
                {
                    LoadExchangeInfo();
                }
            }
        }

        private void LoadExchangeInfo()
        {
            if (Exchange.LoadAccountInfo())
            {
                lblBTCBalance.Text = string.Concat(Exchange.BtcBalance.Value, " BTC");
                lblCurrencyBalance.Text = Exchange.CurrencyBalance.Value.ToString();
            }
            else
            {
                lblStatus.Text = "Error !";
            }
            lblStatus.Text = "Ready";
        }
        private void LoadExchangeInfo(string password)
        {
            if (Exchange.LoadAccountInfo(password))
            {
                lblBTCBalance.Text = string.Concat(Exchange.BtcBalance.Value, " BTC");
                lblCurrencyBalance.Text = Exchange.CurrencyBalance.Value.ToString();
            }
            else
            {
                lblStatus.Text = "Error !";
            }
            lblStatus.Text = "Ready";
        }

        #region Exchange events
        private void Exchange_OnEventChanged(object sender, ExchangeInfoEventArgs e)
        {
            lblStatus.Text = e.Message;
            Application.DoEvents();
        }
        #endregion

        private void tabMain_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (tabMain.SelectedIndex)
            {
                case 0:  // General

                    break;
                case 1:  // Order book

                    break;
                case 2:  // History

                    break;
                case 3:  // Configuration

                    break;
            }
        }

        private void cmdSaveConfiguration_Click(object sender, EventArgs e)
        {
            Exchange.Username = txtMarketUsername.Text.Trim();
            
            // Save password if the user selected:
            if (ddlPassSaveOption.Text == "Use a saved password")
                Exchange.Password = txtSavedPassword.Text.Trim();

            Exchange.Api.ApiKey = txtApiKey.Text;
            Exchange.SaveInfo();
            tabMain.SelectedIndex = 0;
        }
    }
}
